import 'package:flutter/material.dart';
import 'package:robotcontrol/entity/common_item.dart';
import 'package:robotcontrol/utils/cache_image.dart';
import 'package:robotcontrol/utils/date_util.dart';
import 'package:robotcontrol/utils/navigator_util.dart';
import 'package:robotcontrol/utils/share_util.dart';
import 'package:robotcontrol/utils/toast.dart';

class HomeListItemWidget extends StatelessWidget {
  final Item item;

  //是否显示左上角的分类文字：默认显示
  final bool showCategory;

  //是否显示分割线
  final bool showDivider;

  const HomeListItemWidget(
      {Key key, this.item, this.showCategory = true, this.showDivider = true})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    //垂直方向布局
    return Column(
      children: [
        //视频封面图片
        // GestureDetector：手势识别 -- Inkwell
        GestureDetector(
          onTap: () {
            toNamed("/videoDetail", item.data);
          },
          child: Padding(
              padding: EdgeInsets.fromLTRB(15, 10, 15, 10),
              //Stack == FrameLayput
              child: Stack(
                children: [
                  _clipRRectImage(context), //圆角图片
                  _catehoryText(),
                  _videoTime()
                ],
              )),
        ),
        //视频简介
        Container(
          decoration: BoxDecoration(color: Colors.white),
          padding: EdgeInsets.fromLTRB(15, 0, 15, 10),
          child: Row(
            // Row：水平布局，类似Android LinearLayout
            children: [
              //作者头像
              _authorHeaderImage(),
              //简介
              _videoDescription(),
              _shareButton()//分享按钮
            ],
          ),
        )
      ],
    );
  }
  _shareButton(){
    return IconButton(onPressed: ()=>{
      // ToastUtil.toast("msg")
       share(item.data.title,item.data.playUrl)
    }, icon: Icon(Icons.share,color: Colors.black38,));
  }

  //视频简介
  _videoDescription() {
    // Expanded:相当于Android中设置 weight 权重
    return Expanded(
        flex: 1,
        // 垂直的LinearLayout
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              item.data.title,
              style: TextStyle(
                  color: Colors.black,
                  fontSize: 14,
                  fontWeight: FontWeight.bold),
              //过长...
              overflow: TextOverflow.ellipsis,
            ),
            Padding(
              padding: EdgeInsets.only(top: 2),
              child: Text(
                item.data.author == null
                    ? item.data.description
                    : item.data.author.name,
                style: TextStyle(color: Color(0xff9a9a9a), fontSize: 12),
              ),
            )
          ],
        ));
  }

  //作者头像
  _authorHeaderImage() {
    // ClipOval:剪切椭圆，高宽一样则为圆
    return ClipOval(
      clipBehavior: Clip.antiAlias, //抗锯齿
      child: cacheImage(
          item.data.author == null
              ? item.data.provider.icon
              : item.data.author.icon,
          width: 40,
          height: 40),
    );
  }

  //视频时长
  _videoTime() {
    return Positioned(
        right: 15,
        bottom: 10,
        child: ClipRRect(
          borderRadius: BorderRadius.circular(5),
          child: Container(
            decoration: BoxDecoration(color: Colors.black54), //背景
            padding: EdgeInsets.all(5),
            child: Text(
              formatDateMsByMS(item.data.duration * 1000),
              style: TextStyle(
                  color: Colors.white,
                  fontSize: 12,
                  fontWeight: FontWeight.bold),
            ),
          ),
        ));
  }

  //圆角分类
  _catehoryText() {
    // Positioned:用于定位Stack子组件，Positioned必须是Stack的子组件
    return Positioned(
        left: 15,
        top: 10,
        // Opacity:设置透明度，类似于Android中的invisible
        child: Opacity(
          opacity: showCategory ? 1.0 : 0.0, //是否显示
          child: Container(
            decoration: BoxDecoration(
              color: Colors.white54,
              borderRadius: BorderRadius.circular(22),
            ),
            height: 44,
            width: 44,
            alignment: AlignmentDirectional.center,
            //文字居中
            child: Text(
              item.data.category,
              style: TextStyle(fontSize: 14, color: Colors.white),
            ),
          ),
        ));
  }

//圆角图片
  Widget _clipRRectImage(context) {
    return ClipRRect(
      // Hero动画：界面跳转，关联动画
      child: Hero(
        // tag相同的两个widget，跳转时自动关联动画
        tag: '${item.data.id}${item.data.time}',
        child: cacheImage(item.data.cover.feed,
            width: MediaQuery.of(context).size.width, height: 200),
      ),
      borderRadius: BorderRadius.circular(4),
    );
  }
}
